<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="创建日期">
            <div style="display: flex">
              <el-date-picker
                v-model="dateRangeTime.beginTime"
                style="width: 115px"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="date"
                :default-time="[new Date(2000, 1, 1, 0, 0, 0)]"
                placeholder="开始日期"
              />
              <div style="margin: 0 5px">-</div>
              <el-date-picker
                v-model="dateRangeTime.endTime"
                style="width: 115px"
                :default-time="[new Date(2000, 1, 1, 23, 59, 59)]"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="date"
                placeholder="结束日期"
              />
            </div>
          </el-form-item>
          <el-form-item label="模块名称" prop="moduleName">
            <el-input v-model="queryParams.moduleName" placeholder="请输入模块名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="订单名称" prop="orderName">
            <el-input v-model="queryParams.orderName" placeholder="请输入订单名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="分类" prop="cateId">
            <el-input v-model="queryParams.cateId" placeholder="请输入分类" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="材料名称" prop="goodsId">
            <el-input v-model="queryParams.goodsId" placeholder="请输入材料名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['erp:goodsInOutInfo:add']" type="primary" plain icon="Plus">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['erp:goodsInOutInfo:edit']" type="success" plain icon="Edit" :disabled="single">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['erp:goodsInOutInfo:remove']" type="danger" plain icon="Delete" :disabled="multiple">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['erp:goodsInOutInfo:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="goodsInOutInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" width="60" />
        <el-table-column label="模块名称" align="center" prop="moduleName" />
        <el-table-column label="订单名称" align="center" prop="orderName" />
        <el-table-column label="分类" align="center" prop="cateName" />
        <el-table-column label="材料名称" align="center" prop="goodsName" />
        <el-table-column label="原材料规格" align="center" prop="specName" />
        <el-table-column label="原库存" align="center" prop="stock" />
        <el-table-column label="数量" align="center" prop="amount" />
        <el-table-column label="单价" align="center" prop="price" />
        <el-table-column label="小计" align="center" prop="money" />
        <el-table-column label="原因" align="center" prop="remark" width="90" show-overflow-tooltip />
        <el-table-column label="操作人" align="center" prop="updateName" />
        <!--        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
        <!--          <template>-->
        <!--            <el-tooltip content="查看关联" placement="top">-->
        <!--              <el-button v-hasPermi="['erp:goodsInOutInfo:edit']" link type="primary" icon="Edit"></el-button>-->
        <!--            </el-tooltip>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="GoodsInOutInfo" lang="ts">
import { listGoodsInOutInfo, getGoodsInOutInfo, delGoodsInOutInfo, addGoodsInOutInfo, updateGoodsInOutInfo } from '@/api/erp/goodsInOutInfo';
import { GoodsInOutInfoVO, GoodsInOutInfoQuery, GoodsInOutInfoForm } from '@/api/erp/goodsInOutInfo/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const goodsInOutInfoList = ref<GoodsInOutInfoVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const dateRangeTime = ref<any>({
  beginTime: '',
  endTime: ''
});
const queryFormRef = ref<ElFormInstance>();
const goodsInOutInfoFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const data = reactive<any>({
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    moduleName: undefined,
    orderName: undefined,
    goodsId: undefined,
    params: {}
  }
});

const { queryParams } = toRefs(data);

/** 查询进出明细列表 */
const getList = async () => {
  queryParams.value.params = { ...dateRangeTime.value };
  loading.value = true;
  const res = await listGoodsInOutInfo(queryParams.value);
  goodsInOutInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  dateRangeTime.value = {};
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: GoodsInOutInfoVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 删除按钮操作 */
const handleDelete = async (row?: GoodsInOutInfoVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除进出明细编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delGoodsInOutInfo(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'erp/goodsInOutInfo/export',
    {
      ...queryParams.value
    },
    `goodsInOutInfo_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
